<template >
  <!-- 页头 -->
  <div style="margin-left: 20px; margin-top: 20px" id="allOrder">
    <template>
      <!-- 表格 -->
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column prop="id" label="编号"> </el-table-column>
        <el-table-column prop="content" label="评论内容"></el-table-column>
        <el-table-column prop="getDate" label="评论时间"> </el-table-column>
        <el-table-column prop="orderId" label="产品ID"> </el-table-column>
        <el-table-column prop="status" label="状态"> </el-table-column>
        <el-table-column fixed="right" label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="passDiscuss(scope.row)"
              >通过审核</el-button
            >
            <el-button type="text" size="small" @click="delDiscuss(scope.row)"
              >删除</el-button
            >
            <el-button type="text" size="small" @click="noDiscuss(scope.row)"
              >拒绝审核</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页按钮 -->
      <el-pagination
        @current-change="changePage"
        background
        layout="prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </template>
  </div>
</template>
 
<script>
import qs from "querystring";
export default {
  created() {
    this.$axios.defaults.headers.post["Content-Type"] =
      "application/x-www-form-urlencoded";
  },
  data() {
    return {
      tableData: [],
      page: 0,
      pageSize: 6,
      realname: "",
      total: 1,
      telephone: "",
      name: "",
      formLabelWidth: "120px",
    };
  },
  mounted() {
    //页面载入时就发送axios请求
    var obj = {
      page: this.page,
      pageSize: this.pageSize,
    };
    this.$axios.post("/comment/query", qs.stringify(obj)).then((res) => {
      console.log(res);
      //获取用户数量分页
      var total = res.data.data.total;
      this.total = Math.ceil(total / this.pageSize) * 10;
      this.tableData = res.data.data.list;
      console.log(this.tableData);
      this.orderTime();
      //   console.log(this.total);
    });
  },
  methods: {
    //页面改变时重新查询
    changePage(currentPage) {
      console.log(currentPage);
      this.page = currentPage - 1;
      this.getProduct();
    },
    //封装获取评论函数
    getProduct() {
      var obj = {
        page: this.page,
        pageSize: this.pageSize,
      };
      console.log(obj);
      this.$axios.post("/comment/query", qs.stringify(obj)).then((res) => {
        console.log(res);
        //获取用户数量分页
        var total = res.data.data.total;
        this.total = Math.ceil(total / this.pageSize) * 10;
        this.tableData = res.data.data.list;
        console.log(this.tableData);
        this.orderTime();
        //   console.log(this.total);
      });
    },
    //删除评论
    delDiscuss(data) {
      console.log("删除评论");
      var self = this;
      this.$confirm("此操作将永久删除该评论, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          var id = data.id;
          console.log(id);
          self.$axios
            .get("/comment/deleteById", {
              params: {
                id,
              },
            })
            .then((res) => {
              if (res.data.status == 200) {
                self.getProduct();
                self.$message({
                  message: "更新成功",
                  type: "success",
                });
              } else {
                console.log("no");
                self.$message.error("要删除的分类存在关联关系");
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //拒绝审核
    noDiscuss(data) {
      console.log("拒绝审核");
      var self = this;
      this.$confirm("此操作将拒绝审核, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          var id = data.id;
          self.$axios
            .get("/comment/commentRefuseExamine", {
              params: {
                commentid: id,
              },
            })
            .then((res) => {
              console.log(res);
              if (res.data.status == 200) {
                self.getProduct();
                self.$message({
                  message: "更新成功",
                  type: "success",
                });
              } else {
                console.log("no");
                self.$message.error("网络出错");
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    //通过审核
    passDiscuss(data) {
      console.log("通过审核");
      var self = this;
      this.$confirm("此操作将通过审核, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          var id = data.id;
          self.$axios
            .get("/comment/commentExamine", {
              params: {
                commentid: id,
              },
            })
            .then((res) => {
              console.log(res);
              if (res.data.status == 200) {
                self.getProduct();
                self.$message({
                  message: "更新成功",
                  type: "success",
                });
              } else {
                console.log("no");
                self.$message.error("网络出错");
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    //获取时间
    orderTime() {
      for (var i = 0; i < this.tableData.length; i++) {
        var data = new Date(this.tableData[i].commentTime);
        this.tableData[i].getDate = data.toLocaleString();
      }
    },
  },
};
</script>
